﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    using DAL.Entities;

    public class BenhnhanDAL : BaseDAL, UTL.IBaseDAL 
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            string sql = @" SELECT *,(Case when gioitinh_bn = 0 then 'Nam' else N'Nữ' end) as sex,b.doituong FROM benhnhan a join doituong b on a.madt = b.madt ";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @"DELETE FROM benhnhan WHERE ma_bn = '{0}'";
            sql = string.Format(sql,id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (benhnhan)obj;
            string sql = @"set dateformat mdy; INSERT INTO benhnhan(ma_bn,hoten_bn,gioitinh_bn,namsinh_bn,diachi_bn,sdt_bn,madt) VALUES('{0}',N'{1}','{2}','{3}',N'{4}','{5}','{6}') ";
            sql = string.Format(sql,o.ma_bn,o.hoten_bn,o.gioitinh_bn,o.namsinh_bn,o.diachi_bn,o.sdt_bn,o.madt);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (benhnhan)obj;
            string sql = @"set dateformat mdy; UPDATE benhnhan SET hoten_bn=N'{0}', gioitinh_bn='{1}', namsinh_bn='{2}', diachi_bn=N'{3}', sdt_bn='{4}', madt='{5}'WHERE ma_bn='{6}' ";
            sql = string.Format(sql, o.hoten_bn, o.gioitinh_bn, o.namsinh_bn, o.diachi_bn, o.sdt_bn, o.madt,  o.ma_bn);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        public DataTable Select_madt()
        {
            string sql = @" SELECT madt as id, DOITUONG as ten FROM doituong";
            return ExecuteQuery(sql);
        }

        /// <summary>
        /// lay ...
        /// </summary>
        /// <returns></returns>
        public string Select_max()
        {
            string sql = @"SELECT max(ma_bn) as id FROM benhnhan";
            DataTable tb= ExecuteQuery(sql);
            
            return tb.Rows.Count>0? tb.Rows[0]["id"]+"":"0";
        }



        public bool deleteobj(object obj)
        {
            throw new NotImplementedException();
        }
    }
}
